package com.dataseek.iot.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.sys.entity.Dept;
import com.dataseek.iot.sys.entity.Tenant;
import com.dataseek.iot.sys.service.DbService;
import com.dataseek.iot.sys.service.DeptService;
import com.dataseek.iot.sys.vo.DeptVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ObjectResponse;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DbService dbService;

    /**
     * 查询部门列表
     *
     * @param deptName 部门名称
     * @param status 租户状态
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult queryTenant(@RequestParam(value="page")  Integer page, @RequestParam(value="size")  Integer size, @RequestParam(value="deptName", required=false) String deptName, @RequestParam(value="status", required=false)  String status) throws IOException {
        DeptVo tenantVo = deptService.queryDept(deptName,status,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",tenantVo);
    }

    /**
     * 新建部门
     *
     * @param dept 部门实体类
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertTenant(@RequestBody Dept dept) throws IOException {
        Integer code  = deptService.insertDept(dept);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"部门名称重复，请重新输入!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }

    /**
     * 修改部门列表
     *
     * @param dept 租户实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updateDept(@RequestBody Dept dept) throws IOException {
        Integer code  = deptService.updateDept(dept);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }


    /**
     * 删除部门
     *
     * @param ids 部门ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deleteDept(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Integer> listIds = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = deptService.deleteDept(listIds);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"部门使用中，无法删除!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }



}
